﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace DevExpress.MailClient.Win.Controls.Search
{
    public partial class ucAdvancedSearchPanel : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        private INDEX_OPERATION CURRENT_INDEX_OPERATION;
        private enum INDEX_OPERATION
        {
            N_A,
            Equals,
            NotEquals,
            LessThan,
            LessThanOrEquals,
            MoreThan,
            MoreThanOrEquals,
            Contains
        }

        private void GET_INDEX_OPERATIONS(string operation)
        {
            INDEX_OPERATION cURRENT_INDEX_OPERATION = INDEX_OPERATION.N_A;
            switch (operation)
            {
                case "=": cURRENT_INDEX_OPERATION = INDEX_OPERATION.Equals; break;
                case "<": cURRENT_INDEX_OPERATION = INDEX_OPERATION.LessThan; break;
                case "<=": cURRENT_INDEX_OPERATION = INDEX_OPERATION.LessThanOrEquals; break;
                case ">": cURRENT_INDEX_OPERATION = INDEX_OPERATION.MoreThan; break;
                case ">=": cURRENT_INDEX_OPERATION = INDEX_OPERATION.MoreThanOrEquals; break;
                case "!=": cURRENT_INDEX_OPERATION = INDEX_OPERATION.NotEquals; break;
                case "Contains": cURRENT_INDEX_OPERATION = INDEX_OPERATION.Contains; break;
            }

            CURRENT_INDEX_OPERATION = cURRENT_INDEX_OPERATION;
        }

        Dictionary<int, PRO_DOCS.DAL.Database.proc_LOGIN_USERSLoadAllResult> LOGIN_USERS_LIST
        { get; set; }

        public ucAdvancedSearchPanel()
        {
            InitializeComponent();
            ARCHIVE_DOCUMENT_TYPE = new PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE();
            LOGIN_USERS_LIST = new Dictionary<int, PRO_DOCS.DAL.Database.proc_LOGIN_USERSLoadAllResult>();

            int i = 1;

            comboUploadedBy.Properties.Items.Add("Any");
            comboLastChangedBy.Properties.Items.Add("Any");
            PRO_DOCSDatabaseDataContext.proc_LOGIN_USERSLoadAll().ToList().ForEach(new Action<PRO_DOCS.DAL.Database.proc_LOGIN_USERSLoadAllResult>(delegate(PRO_DOCS.DAL.Database.proc_LOGIN_USERSLoadAllResult _loginUser)
            {
                comboUploadedBy.Properties.Items.Add(_loginUser.EMPLOYEE_FULL_NAME);
                comboLastChangedBy.Properties.Items.Add(_loginUser.EMPLOYEE_FULL_NAME);
                LOGIN_USERS_LIST.Add(i, _loginUser);
                i++;
            }));
            comboUploadedBy.SelectedIndex = 0;
            comboLastChangedBy.SelectedIndex = 0;

            comboChangingDateOp.EditValue = "=";
            comboCreationDateOp.EditValue = "=";
        }

        Dictionary<int, PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE> ARCHIVE_DEPARTMENT_DOCUMENT_TYPES
        { get; set; }
        private PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE _aRCHIVE_DOCUMENT_TYPE;
        public PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE ARCHIVE_DOCUMENT_TYPE
        {
            get { return _aRCHIVE_DOCUMENT_TYPE; }
            set
            {
                if (value != null)
                {

                    if (!value.ARCHIVE_DOCUMENT_TYPE_ID.Equals(0))
                    {
                        if (!value.ARCHIVE_DOCUMENT_TYPE_ID.Equals(_aRCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID))
                        {
                            _aRCHIVE_DOCUMENT_TYPE = value;

                            comboDocumentTypes.EditValue = _aRCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_NAME;
                        }
                    }
                    else
                    {
                        _aRCHIVE_DOCUMENT_TYPE = new PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE();
                    }

                }
            }
        }

        private void RELOAD_ARCHIVE_DOCUMENT_TYPES()
        {

            ARCHIVE_DEPARTMENT_DOCUMENT_TYPES = new Dictionary<int, PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE>();

            List<PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE> aRCHIVE_DOCUMENT_TYPES = PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_ARCHIVES_SECURITY.CURRENT_LOGGED_IN_USER_ALLOWED_DOCUMENT_TYPES.Values.ToList();

            comboDocumentTypes.Properties.Items.BeginUpdate();
            comboDocumentTypes.Properties.Items.Clear();

            comboDocumentTypes.Properties.Items.Add("Any");
            int nmbr = 1;
            for (int i = 0; i < aRCHIVE_DOCUMENT_TYPES.Count; i++)
            {
                if (!aRCHIVE_DOCUMENT_TYPES[i].IS_DELETED)
                {
                    PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE aRCHIVE_DOCUMENT_TYPE = aRCHIVE_DOCUMENT_TYPES[i];

                    ARCHIVE_DEPARTMENT_DOCUMENT_TYPES.Add(nmbr, aRCHIVE_DOCUMENT_TYPE);
                    comboDocumentTypes.Properties.Items.Add(aRCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_NAME);
                    nmbr++;
                }
            }
            comboDocumentTypes.SelectedIndex = 0;
            comboDocumentTypes.Properties.Items.EndUpdate();
        }

        private void comboAdvancedDocumentTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            chkIncludePreviusVersion.Visible = false;
            if (comboDocumentTypes.SelectedIndex > -1)
            {
                if (comboDocumentTypes.SelectedIndex.Equals(0))
                {
                    ARCHIVE_DOCUMENT_TYPE = new PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE();
                    tblDynamicInfos.Controls.Clear();
                }
                else
                {
                    chkIncludePreviusVersion.Visible = true;
                    ARCHIVE_DOCUMENT_TYPE = ARCHIVE_DEPARTMENT_DOCUMENT_TYPES[comboDocumentTypes.SelectedIndex];
                    new PRO_DOCS.BLL.WIN.CustomDataTypes().GET_SEARCH_DOCUMENT_TYPE_STRUCTURE(tblDynamicInfos, ARCHIVE_DOCUMENT_TYPE);
                }

            }
            tableLayoutPanel1.Refresh();
        }

        private void ucAdvancedSearchPanel_Load(object sender, EventArgs e)
        {
            RELOAD_ARCHIVE_DOCUMENT_TYPES();
        }

        public List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE> GET_SEARCH_REULTS()
        {
            string aRCHIVE_DOCUMENT_TYPE_ID = (comboDocumentTypes.SelectedIndex > 0) ? ARCHIVE_DEPARTMENT_DOCUMENT_TYPES[comboDocumentTypes.SelectedIndex].ARCHIVE_DOCUMENT_TYPE_ID.ToString() : string.Empty;
            string iNCLUDE_PREVIOUS_VERSIONS = chkIncludePreviusVersion.Checked.ToString();
            string cREATED_BY_DATE = (dteCreationDate.EditValue != null) ? string.Format("'{0}'", dteCreationDate.DateTime.Date.ToString()) : string.Empty;
            string cREATED_BY_OPERATION = comboCreationDateOp.Text;
            string cREATED_BY_USER = (comboUploadedBy.SelectedIndex > 0) ? LOGIN_USERS_LIST[comboUploadedBy.SelectedIndex].LOGIN_USER_ID.ToString() : string.Empty;
            string lAST_MODIFIED_BY_USER = (comboLastChangedBy.SelectedIndex > 0) ? LOGIN_USERS_LIST[comboLastChangedBy.SelectedIndex].LOGIN_USER_ID.ToString() : string.Empty;
            string lAST_MODIFIED_BY_DATE = (dteLastChangingDate.EditValue != null) ? string.Format("'{0}'", dteLastChangingDate.DateTime.Date.ToString()) : string.Empty;
            string lAST_MODIFIED_BY_OPERATION = comboChangingDateOp.Text;

            Dictionary<Guid, PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE> vALID_SEARCHING_FILES = new Dictionary<Guid, PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE>();

            List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE> aRCHIVE_DEPARTMENT_FILES = new List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE>();

            DataSet dsResult = new PRO_DOCS.DAL.AppCode.SearchResults()
                .GET_ADVANCED_SEARCH_RESULTS(aRCHIVE_DOCUMENT_TYPE_ID,
                iNCLUDE_PREVIOUS_VERSIONS,
                cREATED_BY_USER,
                cREATED_BY_DATE,
                cREATED_BY_OPERATION,
                lAST_MODIFIED_BY_USER,
                lAST_MODIFIED_BY_DATE,
                lAST_MODIFIED_BY_OPERATION,
               PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_ARCHIVES_SECURITY.CURRENT_LOGGED_IN_USER_ALLOWED_SEARCH_FOLDERS
                );

            if (string.IsNullOrEmpty(aRCHIVE_DOCUMENT_TYPE_ID))
            {
                vALID_SEARCHING_FILES = new Dictionary<Guid, PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE>();
                for (int i = 0; i < dsResult.Tables[0].Rows.Count; i++)
                {
                    DataRow dtr = dsResult.Tables[0].Rows[i];
                    PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE aRCHIVE_DEPARTMENT_FILE = (from file in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILEs
                                                                                             where file.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Parse(dtr["ARCHIVE_DEPARTMENT_FILE_ID"].ToString()))
                                                                                             select file).FirstOrDefault();
                    vALID_SEARCHING_FILES.Add(aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID, aRCHIVE_DEPARTMENT_FILE);
                }
            }
            else
            {
                //check the values
                Dictionary<Guid, List<string>> sEARCH_DYNAMIC_INDECIES_INPUT_VALUES = new PRO_DOCS.BLL.WIN.CustomDataTypes().GET_SEARCH_DYNAMIC_INDECIES_INPUT_VALUES(tblDynamicInfos);


                Dictionary<Guid, List<string>> FILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES = new Dictionary<Guid, List<string>>();



                foreach (KeyValuePair<Guid, List<string>> _fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES in sEARCH_DYNAMIC_INDECIES_INPUT_VALUES)
                {
                    string search_operator = _fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Value[0];
                    string search_key_value = (_fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Value[1].Equals("Any")) ? string.Empty : _fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Value[1];

                    if (!string.IsNullOrEmpty(search_key_value))
                    {
                        FILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Add(_fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Key, _fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Value);

                    }
                }

                for (int i = 0; i < dsResult.Tables[0].Rows.Count; i++)
                {
                    DataRow dtr = dsResult.Tables[0].Rows[i];

                    bool continue_search = false;

                    PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE aRCHIVE_DEPARTMENT_FILE =
                        (from fileHistory in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILEs
                         where fileHistory.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Parse(dtr["ARCHIVE_DEPARTMENT_FILE_ID"].ToString()))
                         select fileHistory).FirstOrDefault();

                    if (FILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Count.Equals(0))
                    {
                        continue_search = true;
                    }
                    else
                    {
                        bool get_all_indecies = chkIncludePreviusVersion.Checked;



                        List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE> files_history_values = new List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUE>();
                        if (get_all_indecies)
                        {
                            files_history_values = (from file_history_value in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUEs
                                                    where (
                                                              file_history_value.ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ID.Value.Equals(aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID)
                                                          )
                                                    select file_history_value).ToList();


                        }
                        else
                        {
                            PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY lAST_ARCHIVE_DEPARTMENT_FILE_HISTORY = (from file_history in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_HISTORies
                                                                                                                          where file_history.ARCHIVE_DEPARTMENT_FILE_ID.Value.Equals(aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID)
                                                                                                                          orderby file_history.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE descending
                                                                                                                          select file_history).FirstOrDefault();

                            files_history_values = lAST_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEXES_VALUEs.ToList();
                        }

                        List<string> extracted_values = new List<string>();

                        Dictionary<Guid, List<string>> index_values = new Dictionary<Guid, List<string>>();
                        for (int dhv = 0; dhv < files_history_values.Count; dhv++)
                        {
                            if (!index_values.ContainsKey(files_history_values[dhv].ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_ID))
                            {
                                index_values.Add(files_history_values[dhv].ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_ID, new List<string>());
                            }
                            extracted_values = index_values[files_history_values[dhv].ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_ID];
                            extracted_values.Add(files_history_values[dhv].ARCHIVE_DEPARTMENT_FILE_HISTORY_INDEX_VALUE.ToLower());
                            index_values[files_history_values[dhv].ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_ID] = extracted_values;
                        }


                        foreach (KeyValuePair<Guid, List<string>> _fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES in FILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES)
                        {
                            #region SEARCH BY DOCUMENT INDECIES
                            {

                                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX sINGLE_VALUE = (from index in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEXes
                                                                                                             where index.ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_ID.Equals(_fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Key)
                                                                                                             select index).FirstOrDefault();
                                {
                                    PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes inputDataType = (PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes)sINGLE_VALUE.ARCHIVE_DEPARTMENT_DOCUMENT_TYPE_INDEX_DATA_TYPE_ID.Value;

                                    List<string> sEARCH_INDECIES_VALUES_OPERATIONS = sEARCH_DYNAMIC_INDECIES_INPUT_VALUES[_fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Key];

                                    if (index_values.ContainsKey(_fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Key))
                                    {
                                        List<string> sEARCH_INDECIES_KEY_WORD = index_values[_fILTERED_EARCH_DYNAMIC_INDECIES_INPUT_VALUES.Key];

                                        GET_INDEX_OPERATIONS(sEARCH_INDECIES_VALUES_OPERATIONS[0]);

                                        //if (continue_search)
                                        {
                                            switch (CURRENT_INDEX_OPERATION)
                                            {
                                                #region DONE : Equals
                                                case INDEX_OPERATION.Equals:
                                                    {
                                                        for (int kwds = 0; kwds < sEARCH_INDECIES_KEY_WORD.Count; kwds++)
                                                        {
                                                            continue_search = sEARCH_INDECIES_KEY_WORD[kwds].Equals(sEARCH_INDECIES_VALUES_OPERATIONS[1].ToLower());

                                                            if (continue_search)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                #endregion
                                                #region DONE : Not Equals
                                                case INDEX_OPERATION.NotEquals:
                                                    {
                                                        for (int kwds = 0; kwds < sEARCH_INDECIES_KEY_WORD.Count; kwds++)
                                                        {
                                                            continue_search = !sEARCH_INDECIES_KEY_WORD[kwds].Equals(sEARCH_INDECIES_VALUES_OPERATIONS[1].ToLower());

                                                            if (continue_search)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                #endregion
                                                #region DONE : Less Than
                                                case INDEX_OPERATION.LessThan:
                                                    {
                                                        for (int kwds = 0; kwds < sEARCH_INDECIES_KEY_WORD.Count; kwds++)
                                                        {
                                                            switch (inputDataType)
                                                            {
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Numeric:
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Decimal:
                                                                    {
                                                                        continue_search = Convert.ToDecimal(sEARCH_INDECIES_KEY_WORD[kwds]) < Convert.ToDecimal(sEARCH_INDECIES_VALUES_OPERATIONS[1]);
                                                                    }
                                                                    break;
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Date:
                                                                    {
                                                                        if (!string.IsNullOrEmpty(sEARCH_INDECIES_VALUES_OPERATIONS[1]))
                                                                        {
                                                                            List<string> current_default_string = sEARCH_INDECIES_KEY_WORD[kwds].Split('-').ToList();
                                                                            int day = Convert.ToInt32(current_default_string[0]);
                                                                            int month = Convert.ToInt32(current_default_string[1]);
                                                                            int year = Convert.ToInt32(current_default_string[2]);
                                                                            DateTime index_date_time = new DateTime(year, month, day);

                                                                            List<string> current_Search_string = sEARCH_INDECIES_VALUES_OPERATIONS[1].Split('-').ToList();
                                                                            day = Convert.ToInt32(current_Search_string[0]);
                                                                            month = Convert.ToInt32(current_Search_string[1]);
                                                                            year = Convert.ToInt32(current_Search_string[2]);
                                                                            DateTime search_date_time = new DateTime(year, month, day);

                                                                            continue_search = index_date_time < search_date_time;
                                                                        }
                                                                        else
                                                                        {
                                                                            continue_search = false;
                                                                        }
                                                                    }
                                                                    break;
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Time:
                                                                    {
                                                                        if (!string.IsNullOrEmpty(sEARCH_INDECIES_VALUES_OPERATIONS[1]))
                                                                        {
                                                                            continue_search = DateTime.Parse(sEARCH_INDECIES_KEY_WORD[kwds]) < DateTime.Parse(sEARCH_INDECIES_VALUES_OPERATIONS[1]);
                                                                        }
                                                                        else
                                                                        {
                                                                            continue_search = false;
                                                                        }
                                                                    }
                                                                    break;
                                                            }
                                                            if (continue_search)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                #endregion
                                                #region Less Than Or Equals
                                                case INDEX_OPERATION.LessThanOrEquals:
                                                    {
                                                        for (int kwds = 0; kwds < sEARCH_INDECIES_KEY_WORD.Count; kwds++)
                                                        {
                                                            switch (inputDataType)
                                                            {
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Numeric:
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Decimal:
                                                                    {
                                                                        continue_search = Convert.ToDecimal(sEARCH_INDECIES_KEY_WORD[kwds]) <= Convert.ToDecimal(sEARCH_INDECIES_VALUES_OPERATIONS[1]);
                                                                    }
                                                                    break;
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Date:
                                                                    {
                                                                        if (!string.IsNullOrEmpty(sEARCH_INDECIES_VALUES_OPERATIONS[1]))
                                                                        {
                                                                            List<string> current_default_string = sEARCH_INDECIES_KEY_WORD[kwds].Split('-').ToList();
                                                                            int day = Convert.ToInt32(current_default_string[0]);
                                                                            int month = Convert.ToInt32(current_default_string[1]);
                                                                            int year = Convert.ToInt32(current_default_string[2]);
                                                                            DateTime index_date_time = new DateTime(year, month, day);

                                                                            List<string> current_Search_string = sEARCH_INDECIES_VALUES_OPERATIONS[1].Split('-').ToList();
                                                                            day = Convert.ToInt32(current_Search_string[0]);
                                                                            month = Convert.ToInt32(current_Search_string[1]);
                                                                            year = Convert.ToInt32(current_Search_string[2]);
                                                                            DateTime search_date_time = new DateTime(year, month, day);

                                                                            continue_search = index_date_time <= search_date_time;
                                                                        }
                                                                        else
                                                                        {
                                                                            continue_search = false;
                                                                        }
                                                                    }
                                                                    break;
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Time:
                                                                    {
                                                                        if (!string.IsNullOrEmpty(sEARCH_INDECIES_VALUES_OPERATIONS[1]))
                                                                        {
                                                                            continue_search = DateTime.Parse(sEARCH_INDECIES_KEY_WORD[kwds]) <= DateTime.Parse(sEARCH_INDECIES_VALUES_OPERATIONS[1]);
                                                                        }
                                                                        else
                                                                        {
                                                                            continue_search = false;
                                                                        }
                                                                    }
                                                                    break;
                                                            }
                                                            if (continue_search)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                #endregion
                                                #region More Than
                                                case INDEX_OPERATION.MoreThan:
                                                    {
                                                        for (int kwds = 0; kwds < sEARCH_INDECIES_KEY_WORD.Count; kwds++)
                                                        {
                                                            switch (inputDataType)
                                                            {
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Numeric:
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Decimal:
                                                                    {
                                                                        continue_search = Convert.ToDecimal(sEARCH_INDECIES_KEY_WORD[kwds]) > Convert.ToDecimal(sEARCH_INDECIES_VALUES_OPERATIONS[1]);
                                                                    }
                                                                    break;
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Date:
                                                                    {
                                                                        if (!string.IsNullOrEmpty(sEARCH_INDECIES_VALUES_OPERATIONS[1]))
                                                                        {
                                                                            List<string> current_default_string = sEARCH_INDECIES_KEY_WORD[kwds].Split('-').ToList();
                                                                            int day = Convert.ToInt32(current_default_string[0]);
                                                                            int month = Convert.ToInt32(current_default_string[1]);
                                                                            int year = Convert.ToInt32(current_default_string[2]);
                                                                            DateTime index_date_time = new DateTime(year, month, day);

                                                                            List<string> current_Search_string = sEARCH_INDECIES_VALUES_OPERATIONS[1].Split('-').ToList();
                                                                            day = Convert.ToInt32(current_Search_string[0]);
                                                                            month = Convert.ToInt32(current_Search_string[1]);
                                                                            year = Convert.ToInt32(current_Search_string[2]);
                                                                            DateTime search_date_time = new DateTime(year, month, day);

                                                                            continue_search = index_date_time > search_date_time;
                                                                        }
                                                                        else
                                                                        {
                                                                            continue_search = false;
                                                                        }
                                                                    }
                                                                    break;
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Time:
                                                                    {
                                                                        if (!string.IsNullOrEmpty(sEARCH_INDECIES_VALUES_OPERATIONS[1]))
                                                                        {
                                                                            continue_search = DateTime.Parse(sEARCH_INDECIES_KEY_WORD[kwds]) > DateTime.Parse(sEARCH_INDECIES_VALUES_OPERATIONS[1]);
                                                                        }
                                                                        else
                                                                        {
                                                                            continue_search = false;
                                                                        }
                                                                    }
                                                                    break;
                                                            }
                                                            if (continue_search)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                #endregion
                                                #region More Than Or Equals
                                                case INDEX_OPERATION.MoreThanOrEquals:
                                                    {
                                                        for (int kwds = 0; kwds < sEARCH_INDECIES_KEY_WORD.Count; kwds++)
                                                        {
                                                            switch (inputDataType)
                                                            {
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Numeric:
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Decimal:
                                                                    {
                                                                        continue_search = Convert.ToDecimal(sEARCH_INDECIES_KEY_WORD[kwds]) >= Convert.ToDecimal(sEARCH_INDECIES_VALUES_OPERATIONS[1]);
                                                                    }
                                                                    break;
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Date:
                                                                    {
                                                                        if (!string.IsNullOrEmpty(sEARCH_INDECIES_VALUES_OPERATIONS[1]))
                                                                        {
                                                                            List<string> current_default_string = sEARCH_INDECIES_KEY_WORD[kwds].Split('-').ToList();
                                                                            int day = Convert.ToInt32(current_default_string[0]);
                                                                            int month = Convert.ToInt32(current_default_string[1]);
                                                                            int year = Convert.ToInt32(current_default_string[2]);
                                                                            DateTime index_date_time = new DateTime(year, month, day);

                                                                            List<string> current_Search_string = sEARCH_INDECIES_VALUES_OPERATIONS[1].Split('-').ToList();
                                                                            day = Convert.ToInt32(current_Search_string[0]);
                                                                            month = Convert.ToInt32(current_Search_string[1]);
                                                                            year = Convert.ToInt32(current_Search_string[2]);
                                                                            DateTime search_date_time = new DateTime(year, month, day);

                                                                            continue_search = index_date_time >= search_date_time;
                                                                        }
                                                                        else
                                                                        {
                                                                            continue_search = false;
                                                                        }
                                                                    }
                                                                    break;
                                                                case PRO_DOCS.BLL.WIN.CustomDataTypes.InputDataTypes.Time:
                                                                    {
                                                                        if (!string.IsNullOrEmpty(sEARCH_INDECIES_VALUES_OPERATIONS[1]))
                                                                        {
                                                                            continue_search = DateTime.Parse(sEARCH_INDECIES_KEY_WORD[kwds]) >= DateTime.Parse(sEARCH_INDECIES_VALUES_OPERATIONS[1]);
                                                                        }
                                                                        else
                                                                        {
                                                                            continue_search = false;
                                                                        }
                                                                    }
                                                                    break;
                                                            }
                                                            if (continue_search)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                #endregion
                                                #region DONE : N_A : NONE
                                                case INDEX_OPERATION.N_A:
                                                    {
                                                        if (!sEARCH_INDECIES_VALUES_OPERATIONS[1].Equals("ANY"))
                                                        {
                                                            for (int kwds = 0; kwds < sEARCH_INDECIES_KEY_WORD.Count; kwds++)
                                                            {
                                                                continue_search = sEARCH_INDECIES_KEY_WORD[kwds].Contains(sEARCH_INDECIES_VALUES_OPERATIONS[1].ToLower());
                                                                if (continue_search)
                                                                {
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    break;
                                                #endregion
                                                #region DONE : Contains
                                                case INDEX_OPERATION.Contains:
                                                    {
                                                        for (int kwds = 0; kwds < sEARCH_INDECIES_KEY_WORD.Count; kwds++)
                                                        {
                                                            continue_search = sEARCH_INDECIES_KEY_WORD[kwds].Contains(sEARCH_INDECIES_VALUES_OPERATIONS[1].ToLower());
                                                            if (continue_search)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                #endregion
                                            }

                                            if (!continue_search)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }

                            }
                            #endregion
                        }
                    }


                    

                    //passed all the creiterias
                    if (continue_search)
                    {
                        vALID_SEARCHING_FILES.Add(Guid.Parse(dtr["ARCHIVE_DEPARTMENT_FILE_ID"].ToString()), aRCHIVE_DEPARTMENT_FILE);
                    }
                }
            }

            foreach (KeyValuePair<Guid,PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE> item in vALID_SEARCHING_FILES)
            {
                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE aRCHIVE_DEPARTMENT_FILE = item.Value;
                bool continue_search = true;
                #region check the creation date
                if (continue_search)
                {
                    if (!string.IsNullOrEmpty(dteCreationDate.Text))
                    {
                        GET_INDEX_OPERATIONS(comboCreationDateOp.Text);
                        switch (CURRENT_INDEX_OPERATION)
                        {
                            case INDEX_OPERATION.N_A:
                                break;
                            case INDEX_OPERATION.Equals:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date.Equals(dteCreationDate.DateTime.Date);
                                break;
                            case INDEX_OPERATION.NotEquals:
                                continue_search = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date.Equals(dteCreationDate.DateTime.Date);
                                break;
                            case INDEX_OPERATION.LessThan:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date < dteCreationDate.DateTime.Date;
                                break;
                            case INDEX_OPERATION.LessThanOrEquals:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date <= dteCreationDate.DateTime.Date;
                                break;
                            case INDEX_OPERATION.MoreThan:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date > dteCreationDate.DateTime.Date;
                                break;
                            case INDEX_OPERATION.MoreThanOrEquals:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date >= dteCreationDate.DateTime.Date;
                                break;
                            case INDEX_OPERATION.Contains:
                                break;
                            default:
                                break;
                        }
                    }
                }
                #endregion

                #region check the last changing date

                if (continue_search)
                {
                    if (!string.IsNullOrEmpty(dteLastChangingDate.Text))
                    {
                        GET_INDEX_OPERATIONS(comboChangingDateOp.Text);
                        switch (CURRENT_INDEX_OPERATION)
                        {
                            case INDEX_OPERATION.N_A:
                                break;
                            case INDEX_OPERATION.Equals:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date.Equals(dteLastChangingDate.DateTime.Date);
                                break;
                            case INDEX_OPERATION.NotEquals:
                                continue_search = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date.Equals(dteLastChangingDate.DateTime.Date);
                                break;
                            case INDEX_OPERATION.LessThan:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date < dteLastChangingDate.DateTime.Date;
                                break;
                            case INDEX_OPERATION.LessThanOrEquals:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date <= dteLastChangingDate.DateTime.Date;
                                break;
                            case INDEX_OPERATION.MoreThan:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date > dteLastChangingDate.DateTime.Date;
                                break;
                            case INDEX_OPERATION.MoreThanOrEquals:
                                continue_search = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.Date >= dteLastChangingDate.DateTime.Date;
                                break;
                            case INDEX_OPERATION.Contains:
                                break;
                            default:
                                break;
                        }
                    }
                }
                #endregion

                if (continue_search)
                {
                aRCHIVE_DEPARTMENT_FILES.Add(item.Value);

                }
            }


            //aRCHIVE_DEPARTMENT_FILES = vALID_SEARCHING_FILES.Values.ToList();
            return aRCHIVE_DEPARTMENT_FILES;
        }
    }
}
